import login from '../../api/login/index.js'
const loginSotre = {
	namespaced: true,
	state: {
		token: '',
		userInfo: {},
		isAuth: false
	},
	mutations: {
		SET_LOGININFO(state, pyload) {
			state.token = pyload.token
			state.userInfo = pyload.userInfo
			state.isAuth = pyload.isAuth
		},
		SET_USERINFO(state, pyload) {
			state.userInfo = pyload
		}
	},
	actions: {
		setLoginInfo({
			commit
		}, pyload) {
			return new Promise((resolve, reject) => {
				login(pyload).then(res => {
						if (res.code == 200) {
							res.isAuth = true
							commit('SET_LOGININFO', res)
							uni.setStorageSync('token', res.token)
							uni.setStorageSync('userInfo', JSON.stringify(res.userInfo))
							uni.setStorageSync('isAuth', true)
							uni.showToast({
								title: '登录成功'
							})
							resolve(res)
						} else {
							reject(res)
						}
					})
					.catch(err => {
						// 暂时未找到mock返回promise.reject()的方法
						return reject(err)
					})
			})
		}
	}
}

export default loginSotre